home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Sprite 1984 - 1993
/
Sprite 1984 - 1993.iso
/
src
/
lib
/
c
/
netlib
/
RCS
/
Net_InetChecksum.c,v
< prev
next >
Wrap
Text File
|
1990-09-11
|
7KB
|
330 lines
head 1.5;
branch ;
access ;
symbols ;
locks ; strict;
comment @ * @;
1.5
date 90.09.11.14.43.46; author kupfer; state Exp;
branches ;
next 1.4;
1.4
date 89.08.03.15.35.15; author jhh; state Exp;
branches ;
next 1.3;
1.3
date 89.06.19.14.14.29; author jhh; state Exp;
branches ;
next 1.2;
1.2
date 89.03.23.10.17.10; author brent; state Exp;
branches ;
next 1.1;
1.1
date 88.11.21.09.10.15; author mendel; state Exp;
branches ;
next ;
desc
@Formed from net.c of src/lib/old/net.c.
@
1.5
log
@Lint.
@
text
@/*
* Net_InetChecksum.c --
*
* Compute an internet checksum.
*
* Copyright 1987 Regents of the University of California
* All rights reserved.
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies. The University of California
* makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without
* express or implied warranty.
*/
#ifndef lint
static char rcsid[] = "$Header: /sprite/src/lib/net/RCS/Net_InetChecksum.c,v 1.4 89/08/03 15:35:15 jhh Exp Locker: kupfer $ SPRITE (Berkeley)";
#endif not lint
#include "sprite.h"
#include "net.h"
/*
*----------------------------------------------------------------------
*
* Net_InetChecksum --
*
* Compute the 16-bit one's complement of the 1's complement sum of
* of all words in the buffer.
*
* Note: It is assumed that the length of the buffer is at most
* 128K bytes long. It also helps if the buffer is word-aligned.
*
* Results:
* The 1's complement checksum in network byte-order.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
unsigned short
Net_InetChecksum(len, bufPtr)
register int len; /* The number of bytes to checksum. */
Address bufPtr; /* What to checksum. */
{
register unsigned short *wordPtr = (unsigned short *) bufPtr;
register unsigned int sum = 0;
/*
* The basic algorithm 16-bit 1's complement addition is
* 1) add the two unsigned 16-bit quantities,
* 2) if there was a carry out of the high-order bit,
* it is added to the sum.
* To detect a carry out of the high-order bit, the sum is stored
* in a 32-bit word. As an optimization, we delay step 2 until
* all the words have been added together. At that point, the
* upper-half of the sum contains the sum of the carries from the
* additions. This value is then added to the lower half and if that
* operation causes a carry, then 1 is added to the sum.
*
* The optimization does place a limit on how many bytes can be
* summed without causing an overflow of the 32-bit sum. In the worst
* case, a maximum of 64K additions of 16-bit values can be added
* without overflow.
*
* The summation is done in an unrolled loop. Once we have less than
* 32 bytes to sum then it must be done in smaller loops.
*/
if (len == 20) {
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
} else {
while (len >= 32) {
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
sum += *wordPtr++;
len -= 32;
}
while (len >= 2) {
sum += *wordPtr++;
len -= 2;
}
if (len == 1) {
#if BYTE_ORDER == LITTLE_ENDIAN
sum += (*wordPtr) & 0x00ff;
#else
sum += (*wordPtr) & 0xff00;
#endif
}
}
/*
* The most signficant bits of "sum" contains the carries from
* the overflow of the summing. Add this overflow back into
* the least significant 16 bits of the sum and do it a second
* time in case there's a carry from the first time.
*/
if (sum > 0xffff) {
#if 0
extern int main_Debug, tcp_out;
if (tcp_out && main_Debug) {
fprintf(stderr, "Checksum 1: %x\n", sum);
}
#endif /* 0 */
sum = ((sum >> 16) & 0xffff) + (sum & 0xffff);
/*
* See if there was a carry from the addition. The overflow will
* be at most 1.
*/
#if 0
if (tcp_out && main_Debug) {
fprintf(stderr, "Checksum 2: %x\n", sum);
}
#endif /* 0 */
if (sum > 0xffff) {
sum++;
}
}
return((~sum & 0xffff));
}
@
1.4
log
@version 1.3 causes problems with the sun4 dbg module. since no one uses
odd aligned buffers anyway we'll go back to version 1.2
@
text
@d18 1
a18 1
static char rcsid[] = "$Header: /sprite/src/lib/net/RCS/Net_InetChecksum.c,v 1.2 89/03/23 10:17:10 brent Exp Locker: jhh $ SPRITE (Berkeley)";
d132 1
a134 1
/*
d138 2
a139 1
*/
d145 1
a145 1
/*
d149 1
a149 1
*/
@
1.3
log
@Allow buffers to be odd-aligned
@
text
@d18 1
a18 1
static char rcsid[] = "$Header: /sprite/src/lib/net/RCS/Net_InetChecksum.c,v 1.2 89/03/23 10:17:10 brent Exp $ SPRITE (Berkeley)";
a23 1
a44 1
d53 1
a53 7
union {
unsigned char c[2]; /* data as bytes */
unsigned short s; /* data as a word */
} convert;
Boolean oddAligned = FALSE;
a72 9
*
* If the buffer is odd aligned we need to align it first. Save the
* first byte and move the pointer over one. Then do all the sums
* a word at a time. Since we aligned the pointer the bytes in the
* word will be reversed from the aligned case. That's why the saved
* byte is in the 2nd character of the union. If the buffer is an
* odd number of bytes long we store that byte in the first character.
* Then add in the saved characters. Before we return the sum we have
* to swap the bytes into the correct order.
a74 8
convert.s = 0;
if (((int) bufPtr & 1)) {
convert.c[1] = *(unsigned char *) wordPtr;
wordPtr = (unsigned short *) ((char *) wordPtr + 1);
oddAligned = TRUE;
len -= 1;
}
a114 1
}
d116 7
a122 2
if (len == 1) {
convert.c[0] = *((unsigned char *) wordPtr);
a123 1
sum += convert.s;
a151 3
}
if (oddAligned) {
sum = (sum >> 8) | ((sum & 0xff) << 8);
@
1.2
log
@Fixed LITTLE_ENDIAN check. It has to be
#if BYTE_ORDER == LITTLE_ENDIAN
and it cannot be
#ifdef LITTLE_ENDIAN
@
text
@d18 1
a18 1
static char rcsid[] = "$Header: /sprite/src/lib/net/RCS/Net_InetChecksum.c,v 1.1 88/11/21 09:10:15 mendel Exp $ SPRITE (Berkeley)";
d24 1
d46 1
d55 7
a61 1
d81 9
d92 8
d140 1
d142 2
a143 7
if (len == 1) {
#if BYTE_ORDER == LITTLE_ENDIAN
sum += (*wordPtr) & 0x00ff;
#else
sum += (*wordPtr) & 0xff00;
#endif
}
d145 1
d174 3
@
1.1
log
@Initial revision
@
text
@d18 1
a18 1
static char rcsid[] = "$Header: net.c,v 2.0 87/08/11 09:34:20 brent Exp $ SPRITE (Berkeley)";
d117 1
a117 1
#ifdef LITTLE_ENDIAN
@